
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for see_all_network_friends.
	''' </summary>
	Public Partial Class see_all_network_friends
		Inherits BaseWebPage

		Protected lblInterests As System.Web.UI.WebControls.Label
		Protected Literal1 As System.Web.UI.WebControls.Literal
		Protected blankImagesCount As Integer = 0
		Protected currentPageNumber As Integer = 0
		Protected firstPageNumber As Integer = 0
		' the first page of page links
		Protected lastPageNumber As Integer = 0
		Protected pagesRest As Integer = 0
		Protected action As String = "0"
		Protected usrID As Integer = -1


		Public Function CreateDataSource() As ICollection

			Dim usr As BO.User = Users.GetUserByID(usrID)

			Dim startIndex As Integer = Convert.ToInt32(txtStartIndexHidden.Text)
			Dim endIndex As Integer = startIndex + Constants.MAX_PICTURES_ON_PAGE
			Dim i As Integer
			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("DelFriendVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("FriendID", GetType(String)))
			dt.Columns.Add(New DataColumn("FullName", GetType(String)))
			Dim temp As UserPicture
			Dim usrNetworkedFriends As ArrayList = Users.FindNetworkedUsers(usr.ID, 4, False)
			Dim FriendsCount As Integer = usrNetworkedFriends.Count
			Dim imagesOnLastRow As Integer = 0
			Dim t As Integer = 0
			Dim counter As Integer = 0

			txtFriendsCountHidden.Text = FriendsCount.ToString()
			endIndex = Math.Min(endIndex, FriendsCount)
			t = (endIndex - startIndex)
			Math.DivRem(t, Constants.MAX_PICTURES_ON_ROW, imagesOnLastRow)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			counter = 0
			i = 0
			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			For Each nf As BO.NetworkFriend In usrNetworkedFriends
				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()
					temp = nf.[Friend].DefaultPicture

					If temp <> Nothing Then
						dr(0) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(0) = "../images/default_thumbnail.gif"
					End If
					dr(1) = getBeginning(HttpUtility.HtmlEncode(nf.[Friend].Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					If LoggedUsersList.Contains(nf.[Friend].ID) Then
						dr(2) = "../images/online_status.gif"
					Else
						dr(2) = "../images/spacer.gif"
						' init according to the friend's online status
					End If
					If action = Constants.SEE_ALL_FRIENDS_EDIT_ACTION Then

						dr(3) = True
					Else
						dr(3) = False
					End If
					dr(4) = nf.[Friend].ID
					dr(5) = nf.[Friend].Name
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next

			If imagesOnLastRow <> 0 Then
				blankImagesCount = Constants.MAX_PICTURES_ON_ROW - imagesOnLastRow
				i = imagesOnLastRow
				While i < Constants.MAX_PICTURES_ON_ROW
					dr = dt.NewRow()
					'dr[0] = "../images/empty_image.gif";
					dr(0) = "../images/spacer.gif"
					dr(1) = ""
					dr(2) = "../images/spacer.gif"
					dr(3) = "false"
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Public Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.SEE_ALL_NETWORK_FRIENDS_PAGE;

			If LoggedUser = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.SEE_ALL_NETWORK_FRIENDS, "", _Form1, Nothing, Nothing)
			Me.initAdvertisements(_Form1, Nothing)

			' init user whose friends to see: a member ot logged user
			Dim usr As User = Nothing
			If Request.Params("id") <> Nothing Then
				usrID = Convert.ToInt32(Request.Params("id"))
				usr = Users.GetUserByID(usrID)
			Else
				usrID = LoggedUser.ID
				usr = LoggedUser
			End If

			' set add/edit friends visiblity
			If usrID <> LoggedUser.ID Then
				' if member
				imgAddFriends.Visible = False
				lbAddFriends.Visible = False
			End If

			Dim usrNetworkedFriends As ArrayList = Users.FindNetworkedUsers(usr.ID, 4, False)
			Dim FriendsCount As Double = usrNetworkedFriends.Count
			litFirstName.Text = usr.Name
			litFriendsCount.Text = FriendsCount.ToString()
			If FriendsCount = 0 Then
				tblFriends.Visible = False
				lblNoFriends.Visible = True
			Else
				'int MaxPageNumber = Convert.ToInt32(Math.Ceiling( FriendsCount / Constants.MAX_PICTURES_ON_PAGE ));
				dgPager.RedirectPage = Constants.SEE_ALL_NETWORK_FRIENDS
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(FriendsCount / Constants.MAX_PICTURES_ON_PAGE))
				dgPager.Action = action
				dgPagerUp.RedirectPage = Constants.SEE_ALL_NETWORK_FRIENDS
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(FriendsCount / Constants.MAX_PICTURES_ON_PAGE))
				dgPagerUp.Action = action

				' initialize listing literals

				dgPager.dgListing.TotalCount = Convert.ToInt32(FriendsCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(FriendsCount)

				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					txtStartIndexHidden.Text = "0"
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					txtStartIndexHidden.Text = Convert.ToString(((dgPager.CurrentPageNumber - 1) * Constants.MAX_PICTURES_ON_PAGE))
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

				If Not IsPostBack Then
					BindList()
				End If
			End If
		End Sub

		Public Sub BindList()
			dlAllFriends.DataSource = CreateDataSource()
			dlAllFriends.DataBind()

			Dim itemsCount As Integer = dlAllFriends.Items.Count
			Dim blankImageIndex As Integer = itemsCount - blankImagesCount
			Dim i As Integer = 0
			i = blankImageIndex
			While i < itemsCount
				(DirectCast(dlAllFriends.Items(i).FindControl("lbName"), System.Web.UI.WebControls.LinkButton)).CssClass = "empty_image"
				(DirectCast(dlAllFriends.Items(i).FindControl("imgPhoto"), System.Web.UI.WebControls.ImageButton)).CssClass = "empty_image"
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
			Dim stringBuilder As New StringBuilder()
			Dim stringWriter As New StringWriter(stringBuilder)
			Dim htmlWriter As New HtmlTextWriter(stringWriter)
			MyBase.Render(htmlWriter)
			Dim html As String = stringBuilder.ToString()
			Dim start As Integer = html.IndexOf("<form name=""") + 12
			Dim [end] As Integer = html.IndexOf("""", start)
			Dim formID As String = html.Substring(start, [end] - start)
			Dim replace As String = formID.Replace(":", "_")
			html = html.Replace("document." + formID, "document." + replace)
			writer.Write(html)
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlAllFriends.ItemCommand, AddressOf Me.dlAllFriends_ItemCommand

		End Sub
#End Region

		Private Sub dlAllFriends_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim srcType As String = src.[GetType]().ToString()
			Dim cmd As String = e.CommandName
			Dim arg As String = ""
			If srcType.Equals("System.Web.UI.WebControls.ImageButton") Then
				arg = (DirectCast(src, ImageButton)).CommandArgument
			Else
				arg = (DirectCast(src, LinkButton)).CommandArgument
			End If
			Select Case cmd
				Case "Delete"
					Dim usr As BO.User = LoggedUser
					Dim friends As BO.UserUsersList = usr.DirectFriends
					friends.Remove(Convert.ToInt32(arg))
					BindList()
					Exit Select
				Case "MemberRedirect"
					Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
					Exit Select
			End Select
		End Sub

		Protected Sub lbAddFriends_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(Constants.INVITE_FRIENDS_PAGE)
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable _Form1.
		====================
		Public ReadOnly Property _Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated__Form1
			End Get
		End Property
		'set { Migrated__Form1 = value; }
	End Class
End Namespace






